//
//  OMGHomeViewController.m
//  A_project2.0
//
//  Created by Tiny on 16/3/7.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "OMGHomeViewController.h"
#import "OMGHomeModel.h"
#import "OMGHomeTableViewCell.h"
#import "SubjectView.h"
#import "OMGGetMoreViewController.h"
@interface OMGHomeViewController ()<UITableViewDataSource,UITableViewDelegate,pushWebDelegate>
{
    NSInteger _page;
}
@property (nonatomic, strong) UITableView *tableView;                   /**< 创建tabelView */
@property (nonatomic, strong) NSMutableArray *dataArr;                  /**< tableView 数组 */
@property (nonatomic, strong) UIView *headerView;                       /**< tableView头部View */
@property (nonatomic, strong) NSMutableArray *photoAndUrlArr;           /**< 中间图片数组 */
@end

@implementation OMGHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"沈兵兵";

    [self creatTableView];          /**< 创建tableview */
    self.dataArr = [NSMutableArray array];
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_VIEW, HEIGHT_VIEW / 4 * 3)];
    _tableView.tableHeaderView = _headerView;
    [self creatData];               /**< 轮播图数据处理 */
    [self data];                    /**< tableView数据处理 */
    [self creatUI];                 /**< MJ刷新加载 */
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(map)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search)];
}
- (void)map
{
    QPMapViewController *p = [[QPMapViewController alloc]init];
    [self.navigationController pushViewController:p animated:YES];
}
- (void)search
{
    QpSearchViewController *p = [[QpSearchViewController alloc]init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:p];
    [self presentViewController:navi animated:YES completion:^{
        
    }];
}
-(void)creatUI
{
    //刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self data];
    }];
    [header setTitle:@"玩 命 加 载 中..." forState:MJRefreshStateRefreshing];
    [header beginRefreshing];
    self.tableView.mj_header = header;
    // 加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _page++;
        [self data];
    }];
    self.tableView.mj_footer = footer;
}
#pragma mark - 轮播图数据处理
- (void)creatData
{
    [QPNetWorkingTool getNetWorkingTool:@"http://open.qyer.com/qyer/recommands/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88243146147855&lon=121.5398488177902&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&v=1" dic:nil success:^(id result) {
        NSMutableArray *scrollArr = [NSMutableArray array];
        NSMutableArray *urlArr = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"][@"slide"]) {
            [scrollArr addObject:dic[@"photo"]];        // 轮播图图片数组获取
            [urlArr addObject:dic[@"url"]];             // 轮播图点击webview网址获取
        }
        MyLoopsView *scrollView = [MyLoopsView myLoopsViewWithFrame:CGRectMake(0, 0, WIDTH_VIEW, HEIGHT_VIEW / 4) imageArr:scrollArr];
        MyBlock web = ^(NSInteger index)
        {
            self.hidesBottomBarWhenPushed = YES;
            OMGWebViewViewController *wv = [[OMGWebViewViewController alloc]init];
            wv.urlStr = urlArr[index];
            [self.navigationController pushViewController:wv animated:YES];
            self.hidesBottomBarWhenPushed = NO;
        };
        scrollView.block = web;
        [self.headerView addSubview:scrollView];
//        _tableView.tableHeaderView = scrollView;


       
        self.photoAndUrlArr = result[@"data"][@"subject"];
        SubjectView *sub = [[SubjectView alloc]initWithFrame:CGRectMake(0, HEIGHT_VIEW / 4, WIDTH_VIEW, HEIGHT_VIEW / 2)array:_photoAndUrlArr];
        [sub.moreSubjectButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];// 查看更多 btn点击方法
        [self.headerView addSubview:sub];
        sub.delegate = self;
    } fail:^(NSError *error) {
        
    }];
}
-(void)data
{
    [QPNetWorkingTool getNetWorkingTool:[[NSString stringWithFormat:@"http://open.qyer.com/qyer/recommands/trip?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&lat=38.88251788995828&lon=121.5401180455004&page=%ld",(long)_page] stringByAppendingString:@"&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&type=index&v=1"] dic:nil success:^(id result) {
        if (1 == _page) {
            [_dataArr removeAllObjects];
        }
        for (NSDictionary *dic in result[@"data"]) {
            OMGHomeModel *h = [OMGHomeModel modelWithDic:dic];
            [self.dataArr addObject:h];
        }
        [_tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError *error) {
        
    }];
}
#pragma mark - 创建TableView
- (void)creatTableView
{
    // 初始化tabelView
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    // 隐藏滚动条
    _tableView.showsVerticalScrollIndicator = NO;
    // 注册重用池
    [_tableView registerClass:[OMGHomeTableViewCell class] forCellReuseIdentifier:@"cell"];
}
#pragma mark - TableView必须实现的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
// 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMGHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _dataArr[indexPath.row];
    return cell;
}
//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消tableView选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 跳转隐藏 tabbar
    self.hidesBottomBarWhenPushed=YES;
    // 跳转webView
    OMGWebViewViewController *wv = [[OMGWebViewViewController alloc]init];
    OMGHomeModel *h = _dataArr[indexPath.row];
    wv.urlStr = h.view_url;
    [self.navigationController pushViewController:wv animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (void)button:(UIButton*)btn
{
    self.hidesBottomBarWhenPushed = YES;
    OMGGetMoreViewController *g = [[OMGGetMoreViewController alloc]init];
    [self.navigationController pushViewController:g animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
#pragma mark - 协议方法
- (void)pushBigWebWithUrl:(NSString *)urlStr
{
    self.hidesBottomBarWhenPushed = YES;
    OMGWebViewViewController *wv = [[OMGWebViewViewController alloc]init];
    wv.urlStr = urlStr;
    [self.navigationController pushViewController:wv animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
